package com.seek.educms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seek.commonutils.ResponseResult;
import com.seek.educms.entity.CrmBanner;
import com.seek.educms.entity.vo.BannerQuery;
import com.seek.educms.service.CrmBannerService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页 banner表 后台前端控制器
 * </p>
 *
 * @author wml
 * @since 2022-02-10
 */
@RestController
@RequestMapping("/educms/banner/admin")
public class CrmBannerAdminController {

    @Autowired
    private CrmBannerService crmBannerService;

    // 分页查询
    @ApiOperation(value = "获取 Banner 分页列表")
    @GetMapping("pageBanner/{current}/{size}")
    public ResponseResult pageBanner(@PathVariable long current, @PathVariable long size) {
        Page<CrmBanner> page = new Page<>(current, size);
        crmBannerService.page(page);
        long total = page.getTotal();
        List<CrmBanner> records = page.getRecords();
        return ResponseResult.ok().data("total", total).data("records", records);
    }

    // 分页查询带条件
    @ApiOperation(value = "获取 Banner 分页列表，带条件")
    @GetMapping("pageBannerCondition/{current}/{size}")
    public ResponseResult pageBannerCondition(@PathVariable long current, @PathVariable long size, BannerQuery bannerQuery) {
        Page<CrmBanner> page = new Page<>(current, size);
        QueryWrapper<CrmBanner> queryWrapper = new QueryWrapper<>();
        String title = bannerQuery.getTitle();
        Integer isShow = bannerQuery.getIsShow();
        String begin = bannerQuery.getBegin();
        String end = bannerQuery.getEnd();
        if (title != null && !title.equals("")) {
            queryWrapper.like("title", title);
        }
        if (isShow != null) {
            queryWrapper.eq("is_show", isShow);
        }
        if (begin != null && !begin.equals("")) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (end != null && !end.equals("")) {
            queryWrapper.le("gmt_create", end);
        }
        crmBannerService.page(page, queryWrapper);
        long total = page.getTotal();
        List<CrmBanner> records = page.getRecords();
        return ResponseResult.ok().data("total", total).data("records", records);
    }

    @ApiOperation(value = "获取 Banner，根据 id")
    @GetMapping("get/{id}")
    public ResponseResult get(@PathVariable String id) {
        CrmBanner banner = crmBannerService.getById(id);
        return ResponseResult.ok().data("item", banner);
    }

    @ApiOperation(value = "新增Banner")
    @PostMapping("save")
    public ResponseResult save(@RequestBody CrmBanner banner) {
        crmBannerService.saveBanner(banner);
        return ResponseResult.ok();
    }

    @ApiOperation(value = "修改Banner")
    @PutMapping("update")
    public ResponseResult updateById(@RequestBody CrmBanner banner) {
        crmBannerService.updateBannerById(banner);
        return ResponseResult.ok();
    }

    @ApiOperation(value = "删除Banner")
    @DeleteMapping("remove/{id}")
    public ResponseResult remove(@PathVariable String id) {
        crmBannerService.removeBannerById(id);
        return ResponseResult.ok();
    }

}

